package cn.iocoder.yudao.module.book.controller.admin.book;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.book.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.book.BookDO;
import cn.iocoder.yudao.module.book.service.book.BookService;

@Tag(name = "图书管理系统 - 图书")
@RestController
@RequestMapping("/book/book")
@Validated
public class BookController {

    @Resource
    private BookService Service;

    @PostMapping("/create")
    @Operation(summary = "创建图书")
    @PreAuthorize("@ss.hasPermission('book::create')")
    public CommonResult<Long> create(@Valid @RequestBody BookSaveReqVO createReqVO) {
        return success(Service.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新图书")
    @PreAuthorize("@ss.hasPermission('book::update')")
    public CommonResult<Boolean> update(@Valid @RequestBody BookSaveReqVO updateReqVO) {
        Service.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除图书")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book::delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        Service.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得图书")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book::query')")
    public CommonResult<BookRespVO> get(@RequestParam("id") Long id) {
        BookDO bookDO = Service.get(id);
        return success(BeanUtils.toBean(bookDO, BookRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得图书分页")
    @PreAuthorize("@ss.hasPermission('book::query')")
    public CommonResult<PageResult<BookRespVO>> getPage(@Valid BookPageReqVO pageReqVO) {
        PageResult<BookDO> pageResult = Service.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BookRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出图书 Excel")
    @PreAuthorize("@ss.hasPermission('book::export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid BookPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookDO> list = Service.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "图书.xls", "数据", BookRespVO.class,
                        BeanUtils.toBean(list, BookRespVO.class));
    }

}